package com.sjgtw.cloud.system.controller;

import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxProvisionResult;
import com.sjgtw.cloud.common.log.annotation.Log;
import com.sjgtw.cloud.common.log.enums.BusinessType;
import com.sjgtw.cloud.system.api.domain.SysDept;
import com.sjgtw.cloud.system.domain.orm.UserEntity;
import com.sjgtw.cloud.system.domain.vo.ProvisioningVo;
import com.sjgtw.cloud.system.service.ISysDeptService;
import com.sjgtw.cloud.system.service.ISysUserProvisioningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title: 用户数据同步接口实现</p>
 * <p>Description: </p>
 * @author Admin
 * @date 2022年7月25日
 */
@RestController
@RequestMapping("/provisioning/rsf/user")
public class ProvisioningUserRSFController extends BaseController
{
    @Autowired
    private ISysUserProvisioningService userService;
    
    @Autowired
	private ISysDeptService deptService;

    /**
     * <p>Title: 创建新账号</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "用户数据同步(rsf)", businessType = BusinessType.INSERT)
    @PostMapping("/addAccount")
    public AjaxProvisionResult add(@RequestBody ProvisioningVo ps)
    {
    	UserEntity user = ps.getUserEntity();
    	UserEntity sysUser = userService.selectUserByUserName(user.getUserName());
		//组织数据同步
		if(StringUtils.isNotEmpty(user.getDeptNum())) {
	    	SysDept dept = deptService.selectDeptByDeptNum(user.getDeptNum());
	    	if(StringUtils.isNotNull(dept)) {
	    		user.setDeptId(dept.getDeptId());
	    		user.setDeptNum(dept.getDeptNum());
	    		user.setDeptName(dept.getDeptName());
	    	}
			user.setDeptNum(user.getDeptNum());
		}
    	user.setUserType("01");
    	//修改用户
    	if (StringUtils.isNotNull(sysUser))
        {
    		user.setUserId(sysUser.getUserId());
    		user.setUpdateBy("rsf身份供应同步");
    		user.setUpdateTime(ps.getTimeStamp());
    		int rows = userService.updateUser(user);
    		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"新增用户成功")
    				: AjaxProvisionResult.error(ps.getRequestID(),"userexception-002","新增账户出现错误");
        }
        user.setCreateBy("rsf身份供应同步");
        user.setCreateTime(ps.getTimeStamp());
        int rows = userService.insertUser(user);
    	return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"新增用户成功")
				: AjaxProvisionResult.error(ps.getRequestID(),"userexception-002","新增账户出现错误");
    }

    /**
     * <p>Title: 修改账号信息</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "用户数据同步(rsf)", businessType = BusinessType.UPDATE)
    @PutMapping("/modifyAccount")
    public AjaxProvisionResult edit(@RequestBody ProvisioningVo ps)
    {
    	UserEntity user = ps.getUserEntity();
    	UserEntity sysUser = userService.selectUserByUserName(user.getUserName());
    	//用户不存在
    	if (StringUtils.isNull(sysUser))
        {
    		return AjaxProvisionResult.error(ps.getRequestID(),"userexception-010","账户不存在");
        }
		//组织数据同步
		if(StringUtils.isNotEmpty(user.getDeptNum())) {
	    	SysDept dept = deptService.selectDeptByDeptNum(user.getDeptNum());
	    	if(StringUtils.isNotNull(dept)) {
	    		user.setDeptId(dept.getDeptId());
	    		user.setDeptNum(dept.getDeptNum());
	    		user.setDeptName(dept.getDeptName());
	    	}
			user.setDeptNum(user.getDeptNum());
		}
    	user.setUserType("01");
    	user.setUserId(sysUser.getUserId());
		user.setUpdateBy("rsf身份供应同步");
		user.setUpdateTime(ps.getTimeStamp());
		int rows = userService.updateUser(user);
		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"修改账户成功")
				: AjaxProvisionResult.error(ps.getRequestID(),"userexception-003","修改账户信息出现错误");
    }
    
    /**
     * <p>Title: 禁用账号</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "用户数据同步(rsf)", businessType = BusinessType.GRANT)
    @PostMapping("/suspendAccount")
    public AjaxProvisionResult suspendAccount(@RequestBody ProvisioningVo ps)
    {
    	UserEntity user = ps.getUserEntity();
    	UserEntity sysUser = userService.selectUserByUserName(user.getUserName());
    	//用户不存在
    	if (StringUtils.isNull(sysUser))
        {
    		return AjaxProvisionResult.error(ps.getRequestID(),"userexception-010","账户不存在");
        }

    	user.setUserId(sysUser.getUserId());
    	user.setStatus("1");
		user.setUpdateBy("rsf身份供应同步");
		user.setUpdateTime(ps.getTimeStamp());
        int rows = userService.updateUserStatus(user);
		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"禁用帐户成功")
				: AjaxProvisionResult.error(ps.getRequestID(),"userexception-005","禁用帐户出现错误");
    }

    /**
     * <p>Title: 启用账号</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "用户数据同步(rsf)", businessType = BusinessType.GRANT)
    @PostMapping("/restoreAccount")
    public AjaxProvisionResult restoreAccount(@RequestBody ProvisioningVo ps)
    {
    	UserEntity user = ps.getUserEntity();
    	UserEntity sysUser = userService.selectUserByUserName(user.getUserName());
    	//用户不存在
    	if (StringUtils.isNull(sysUser))
        {
    		return AjaxProvisionResult.error(ps.getRequestID(),"userexception-010","账户不存在");
        }

    	user.setUserId(sysUser.getUserId());
    	user.setStatus("0");
		user.setUpdateBy("rsf身份供应同步");
		user.setUpdateTime(ps.getTimeStamp());
        int rows = userService.updateUserStatus(user);
		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"启用帐户成功")
				: AjaxProvisionResult.error(ps.getRequestID(),"userexception-006","启用帐户出现错误");
    }


    /**
     * <p>Title: 删除账号</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "用户数据同步(rsf)", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteAccount")
    public AjaxProvisionResult deleteAccount(@RequestBody ProvisioningVo ps)
    {
    	UserEntity user = ps.getUserEntity();
    	UserEntity sysUser = userService.selectUserByUserName(user.getUserName());
    	//用户不存在
    	if (StringUtils.isNull(sysUser))
        {
    		return AjaxProvisionResult.error(ps.getRequestID(),"userexception-010","账户不存在");
        }

    	user.setUserId(sysUser.getUserId());
    	user.setDelFlag("2");
		user.setUpdateBy("rsf身份供应同步");
		user.setUpdateTime(ps.getTimeStamp());
        int rows = userService.updateUserDelFlag(user);
		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"删除用户成功")
				: AjaxProvisionResult.error(ps.getRequestID(),"userexception-004","删除用户出错");
    }

    /**
     * <p>Title: 查询账户列表</p>
     * <p>Description: </p>
     * @param user
     * @return
     */
    @Log(title = "用户数据同步(rsf)", businessType = BusinessType.OTHER)
    @GetMapping("/searchAccount")
    public AjaxProvisionResult searchAccountList(@RequestBody ProvisioningVo ps)
    {
    	UserEntity userSel = ps.getUserEntity();
    	UserEntity user = new UserEntity();
    	if(StringUtils.isNotNull(userSel)) {
    		user.setUserName(replaceUE(userSel.getUserName()));
    		user.setNickName(replaceUE(userSel.getNickName()));
    		//备用
    		user.setPhonenumber(replaceUE(userSel.getPhonenumber()));
    		user.setLinephone(replaceUE(userSel.getLinephone()));
    		user.setFirstName(replaceUE(userSel.getFirstName()));
    		user.setLastName(replaceUE(userSel.getLastName()));
    		user.setOrgCode(replaceUE(userSel.getOrgCode()));
    		user.setOrgName(replaceUE(userSel.getOrgName()));
    		user.setDeptNum(replaceUE(userSel.getDeptNum()));
    		user.setDeptName(replaceUE(userSel.getDeptName()));
    	}
        List<UserEntity> list = userService.selectUserList(user);
        return AjaxProvisionResult.success(ps.getRequestID(),"查询账户成功",list);
    }

    /**
     * <p>Title: changePassword</p>
     * <p>Description: 修改用户密码，暂不实现本接口</p>
     * @param ps
     * @return
     */
    @Log(title = "用户数据同步(rsf)", businessType = BusinessType.OTHER)
    @PostMapping("/changePassword")
    public AjaxProvisionResult changePassword(@RequestBody ProvisioningVo ps)
    {
    	return AjaxProvisionResult.error(ps.getRequestID()," userexception-008","业务系统未实现该接口");
    }

    private String replaceUE(String Str) {
    	if(StringUtils.isNotEmpty(Str)) {
    		return Str.replace("*", "%");
    	}
    	return "";
    }
}
